

//
//  MyTableViewController.m
//  斐波那契数列
//
//  Created by yang on 2017/12/30.
//  Copyright © 2017年 YYY. All rights reserved.
//

#import "MyTableViewController.h"

@interface MyTableViewController ()

@property(nonatomic,strong)NSArray *numberList;

@property(nonatomic,assign)BOOL ascend;

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ascend = YES;
    
    self.title = @"斐波那契数列";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Ascend" style:UIBarButtonItemStylePlain target:self action:@selector(sort)];
    
    [self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:@"cell"];
    [self loadData:^(NSArray *numbers) {
        
        self.numberList = numbers;
        [self.tableView reloadData];
    }];
    
    //遍历view中的所有控件
    [self findSubView:self.view];
    
}


- (void)findSubView:(UIView *)view
{
    for (UIView *subView in view.subviews)
    {
        NSLog(@"子视图： %@",subView);
        [self findSubView:subView];
    }
}
//排序
- (void)sort
{
    self.ascend = !self.ascend;
    self.navigationItem.rightBarButtonItem.title = self.ascend ? @"Ascend" : @"Desc";
    self.numberList = [self.numberList sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        //return [obj1 integerValue] < [obj2 integerValue];
        
        return self.ascend ? ([obj1 integerValue] > [obj2 integerValue]) : ([obj1 integerValue] < [obj2 integerValue]);
    }];
    [self.tableView reloadData];
}

//加载数据
- (void)loadData:(void(^)(NSArray *numbers))finished
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSArray *newArray = [self fibonacii:INT8_MAX];
        dispatch_async(dispatch_get_main_queue(), ^{
            finished(newArray);
        });
    });
}

//斐波那契数列
- (NSArray *)fibonacii:(int)number
{
    if (number < 2)
    {
        return nil;
    }
    NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:@"0", @"1", nil];
    for (int i=2; i<number; i++)
    {
        NSInteger number1 = [tempArray[i-1] integerValue];
        NSInteger number2 = [tempArray[i-2] integerValue];
        if (number1 + number2 > UINT8_MAX)
        {
            break;
        }
        [tempArray addObject:@(number1 + number2)];
    }
    return tempArray;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.numberList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify forIndexPath:indexPath];
    NSInteger row = indexPath.row;
    if (!self.ascend)
    {
        row = self.numberList.count - row - 1;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"行：%ld        斐波那契数列：%@",row,self.numberList[indexPath.row]];
    return cell;
}

@end
